/**
 * %项目描述%
 * %版本信息%
 */
package com.leehom.arch.base.shard.model;

import java.io.Serializable;

import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;

/**
 * @类名: Shard
 * @说明: 分片，分片可以是任何资源，数据库，文件
 *        分片由3个标记(marker)确定边界
 *        start 起始边界(包括)
 *        current 当前处理位置
 *        end 结束边界(不包括)，可无
 * 
 * @author leehom
 * 
 * @Date 2012-6-16 下午10:59:18
 * 
 * @see
 */
@Entity
@Table(name = "t_base_shard")
@Data
public class Shard implements Serializable {
	
	/** */
	private static final long serialVersionUID = -8791953322896209416L;
	
	/** marker主键*/
	@EmbeddedId
	private ShardId id;
	/** 处理批量规模*/
	@Column(name = "batch_size")
	private Integer batchSize;
	/** 标记类型*/
	@Enumerated(EnumType.STRING)  
	private MarkerType type;
	/** 起始位置标记(包括)*/
	@Embedded
	@AttributeOverrides({
		 @AttributeOverride(name="name", column=@Column(name="start_name")),
		 @AttributeOverride(name="nvalue", column=@Column(name="start_nvalue")),
		 @AttributeOverride(name="dvalue", column=@Column(name="start_dvalue", columnDefinition="datetime(3)")),
		 @AttributeOverride(name="svalue", column=@Column(name="start_svalue"))
	 })
	private Marker start;
	/** 当前位置标记，其中vlues为标记的阀值*/
	@Embedded
	@AttributeOverrides({
		 @AttributeOverride(name="name", column=@Column(name="current_name")),
		 @AttributeOverride(name="nvalue", column=@Column(name="current_nvalue")),
		 @AttributeOverride(name="dvalue", column=@Column(name="current_dvalue", columnDefinition="datetime(3)")),
		 @AttributeOverride(name="svalue", column=@Column(name="current_svalue"))
	 })
	private Marker current;
	/** 结束位置标记(不包括)*/
	@Embedded
	@AttributeOverrides({
		 @AttributeOverride(name="name", column=@Column(name="_name")),
		 @AttributeOverride(name="nvalue", column=@Column(name="end_nvalue")),
		 @AttributeOverride(name="dvalue", column=@Column(name="end_dvalue", columnDefinition="datetime(3)")),
		 @AttributeOverride(name="svalue", column=@Column(name="end_svalue"))
	 })
	private Marker end;
	
	public Shard() {
		super();
	}
	
	public Shard(int batchSize, Marker start, Marker current, Marker end, MarkerType type) {
		super();
		this.batchSize = batchSize;
		this.start = start;
		this.current = current;
		this.end = end;
		this.type = type;
	}

	// 获取当前标记
	public Object getCurValue() {
		return this.getMarkerValue(current);
		
	}
	
	// 获取开始标记
	public Object getStartValue() {
		return this.getMarkerValue(start);
		
	}
	
	// 获取结束标记
	public Object getEndValue() {
		if(end==null)
			return null;
		return this.getMarkerValue(end);
	}
	
	public void setCurrent(Marker current) {
		this.current = current;
	}
	
	// 分片是否处理完
	public boolean isEnd() {
		return current.equals(end);
	}
	
	public void setStart(Marker start) {
		this.start = start;
		this.current = copy(start);
	}
	
	public void setEnd(Marker end) {
		this.end = end;
	}

	// 获取标记值
	private Object getMarkerValue(Marker marker) {
		if(this.getType()==MarkerType.D)
			return marker.getDvalue();
		if(this.getType()==MarkerType.L)
			return marker.getNvalue();
		if(this.getType()==MarkerType.S)
			return marker.getSvalue();
		return null;
		
	}
	
	private Marker copy(Marker marker) {
		Marker newMarker = new Marker();
		newMarker.setName(marker.getName());
		newMarker.setDvalue(marker.getDvalue());
		newMarker.setNvalue(marker.getNvalue());
		newMarker.setSvalue(marker.getSvalue());
		return newMarker;
	}

}
